
#include <tslib.h>
#include <input_manager.h>
#include <stdio.h>

static struct tsdev *ts; // tsdev是tslib中的结构体


static int TS_GetInputEvent(PInputEvent ptInputEvent){

    struct ts_sample samp;
    int ret; // 用于记录返回值


    ret = ts_read(ts, &samp, 1);
    if (ret != 1)
    {   
        printf("ts_read failed.\n");
        return -1; // 获取失败返回-1
    }

    ptInputEvent->iType = INPUT_TYPE_TOUCH;
    ptInputEvent->iX = samp.x;
    ptInputEvent->iY = samp.y;
    ptInputEvent->iPressure = samp.pressure;
    ptInputEvent->tTime = samp.tv;
    
    return 0;
}


static int TS_InputDeviceInit(void){

    ts = ts_setup(NULL, 0); // 初始化触摸屏设备
	if (!ts)
	{
		printf("ts_setup failed.\n");
		return -1;
	}

    // printf("ts_setup successed.\n");
    return 0; // 初始化成功返回0
}



static int TS_InputDeviceExit(void){
    ts_close(ts);
    return 0;
}




// 声明一个InputDevice结构体，用来描述触屏输入设备
static struct InputDevice g_tTouchScreen = {
    
    .name = "TouchScreen",
    .InputDeviceInit = TS_InputDeviceInit,
    .GetInputEvent = TS_GetInputEvent,
    .InputDeviceExit = TS_InputDeviceExit,

};


void RegisterTStoDeviceList(void)
{

	// g_tTouchScreen调用注册InputEvent函数
	RegisterInputDevice(&g_tTouchScreen);
}


#if 0

// 测试触屏输入
int main(int argc, char **argv)
{
    InputEvent tInputEvent_tmp; // 定义一个tmp的PInputEvent用于测试
    int ret;

    // 初始化
    g_tTouchScreen.InputDeviceInit();

    while (1)
    {
        ret = g_tTouchScreen.GetInputEvent(&tInputEvent_tmp);
        if (ret)
        {
            printf("GetInputEvent failed.\n");
            return -1;
        }
        else
        {
            printf("iType = %d\n", tInputEvent_tmp.iType);
            printf("iX = %d\n", tInputEvent_tmp.iX);
            printf("iY = %d\n", tInputEvent_tmp.iY);
            printf("iPressure = %d\n", tInputEvent_tmp.iPressure);
        }
    }
    
    return 0;
}


#endif
